﻿<?php
/**
 * ShopEx licence 
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class omeanalysts_ctl_analysis_logireport extends desktop_controller{
    
    var $workground = 'omeanalysts_ctl_analysis_logireport';
    var $pagelimit = 10;
    public function __construct($app)
    {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

   function index()
   {
        $begin = strtotime($_POST['begin_date']);
        $end = strtotime($_POST['end_date']);
        $_POST['begin_date'] = $_POST['begin_date'] . ' ' . $_POST['_DTIME_']['H']['begin_date'] . ':' . $_POST['_DTIME_']['M']['begin_date'];
        $begin = strtotime($_POST['begin_date']);
        $_POST['end_date'] = $_POST['end_date'] . ' ' . $_POST['_DTIME_']['H']['end_date'] . ':' . $_POST['_DTIME_']['M']['end_date'];
        $end = strtotime($_POST['end_date']);
        $_POST['begin_date'] = $_POST['begin_date'] ? $_POST['begin_date'] : $begin_date;
        $_POST['end_date'] = $_POST['end_date'] ? $_POST['end_date'] : $end_date;
        if ($_POST['begin_date']) {
            $_POST['begin_date'] = strtotime($_POST['begin_date']);
            $_POST['begin_date'] = date("Y-m-d H:i:s", $_POST['begin_date']);
        }
        if ($_POST['end_date']) {
            $_POST['end_date'] = strtotime($_POST['end_date']);
            $_POST['end_date'] = date("Y-m-d H:i:s", $_POST['end_date']);
        }

        $shopObj = app::get('ome')->model('shop');
        if($this->shopIds){
            $sql = 'SELECT shop_id,shop_bn,name FROM sdb_ome_shop WHERE shop_id IN (\''.implode('\',\'',$this->shopIds).'\') order by shop_bn';
            $shop_datas = kernel::database()->select($sql);
        }else{
            $shop_datas = $shopObj->getList('shop_id,shop_bn,name',array(),0,-1);
        }
        $shop_data[0] = '全部';
        foreach ($shop_datas as $v) {
            $shop_data[$v['shop_id']]  = '【'.$v['shop_bn'].'】'.$v['name'];
        }

        //add by IVAN20190216 根据判断过滤权限
        $Obranch = app::get('ome')->model('branch');
        $branch_ids = $Obranch->getList('branch_id,name,uname,phone,mobile');
        //$branch_data[0]='全部';
        foreach ($branch_ids as $key => $value) {
            $branch_data[$value['branch_id']]=$value['name'];
        }
    
        #统计日期
        if ($_POST['begin_date'] == '1970-01-01 08:00:00' || !$_POST['begin_date']) {

            $begin_date = strtotime(date("Y-m-d H:i:s", time()));
            $clearingtable['begin_date'] = $begin_date;
        } else {
            $begin_date = $_POST['begin_date'];
            $clearingtable['begin_date'] = $begin;
        }
        if ($_POST['end_date'] == '1970-01-01 08:00:00' || !$_POST['begin_date']) {
            $end_date = strtotime(date("Y-m-d H:i:s", time()));
            $clearingtable['end_date'] = $end_date;
        } else {
            $end_date = $_POST['end_date'];
            $clearingtable['end_date'] = $end;
        }
        /*# 获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) {
            $oBranch = app::get('ome')->model('branch');
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $base_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                $base_filter['ext_branch_id'] = array_intersect($base_filter['ext_branch_id'], $ownerBranch);
            } else {
                $base_filter['ext_branch_id'] = 'false';
            }
        } else {
            $base_filter['ext_branch_id'] = $ownerBranch;
        }*/

        $clearingtable['logi_name'] = $_POST['logi_name'];
        $clearingtable['shop_id'] = $_POST['shop_id'];
        $clearingtable['branch_id'] = $_POST['branch_id'];
        $logidataObj = app::get('wms')->model('deliverycomp');
        $logi_data = $logidataObj->GetSData_logireport($begin_date,$end_date,$_POST['shop_id'],$_POST['logi_name'],$_POST['branch_id']);
        $this->pagedata['clearingtable'] = $clearingtable;
        $this->pagedata['shop_data'] = $shop_data;
        $this->pagedata['logi_data'] = $logi_data;
        $this->pagedata['branch_data'] = $branch_data;
       
        $this->page('analysis/logireport/reportshow.html');
        
    }
  
    function report()
    {
        $begin_date = substr($_GET['begin_date'], 0, 10);
        $end_date = substr($_GET['end_date'], 0, 10);
        $where = array('active' => 'true');
        set_time_limit(600);
        $logidata_obj = app::get('wms')->model('deliverycomp');
        $begin_date = date("Y-m-d H:i:s", $begin_date);
        $end_date = date("Y-m-d H:i:s", $end_date);
        $clearingtable['logi_name'] = $_GET['logi_name'];
        $clearingtable['shop_id'] = $_GET['shop_id'];

        $comp_data = $logidata_obj->GetSData_logireport($begin_date,$end_date,$_GET['shop_id'],$_GET['logi_name'],$_GET['branch_id']);
        include_once ROOT_DIR . "/custom/lib/phpExcel.php";

        $excel = new phpExcel();
        $excel->addHeader(array(
            '店铺编码','店铺名称','发货单号', '发货时间', '物流单号','重量','收货人姓名',
            '收货人手机','收货人电话','收货地区','收货地址','仓库'
        ));
        if ($comp_data) {
            $data_xls = array();

            foreach ($comp_data as $val) {
                $data_xls[] = array(
                    $val['shop_bn'],
                    $val['shop_name'],
                    $val['delivery_bn'],
                    $val['delivery_time'],
                    //$val['logi_no'],
                    $val['logi_no'] = "=\"".$val['logi_no']."\"",
                    $val['weight'] ? $val['weight'] : 0,
                    $val['ship_name'],
                    $val['ship_mobile'],
                    $val['ship_tel'],
                    $val['ship_area'],
                    $val['ship_addr'],
                     $val['b_name']
                   
                );
            }
        }
        $excel->addBody(
            $data_xls
        );

        $excel->downLoad();
        exit;
    }

}
